package com.example.proto3;

import com.google.protobuf.InvalidProtocolBufferException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2023/12/17 17:37
 */
public class TestRead {
    public static void main(String[] args) throws IOException {
        //读取文件，并将读取的内容进行反序列化操作
        Contacts contacts = Contacts.parseFrom(new FileInputStream("src/main/java/com/example/proto3/contacts.bin"));


        //打印操作
        printContacts(contacts);
//        System.out.println(contacts.toString());

    }

    private static void printContacts(Contacts contacts) throws InvalidProtocolBufferException {
        int i = 1;
        for(PeopleInfo peopleInfo : contacts.getContactsList()){
            System.out.println("-------------联系人"+i++ +"-----------" );
            System.out.println(peopleInfo.getName());
            System.out.println(peopleInfo.getAge());
            int j = 1;
            for(PeopleInfo.Phone phone:peopleInfo.getPhoneList()){
                System.out.println("电话"+j++ +": "+ phone.getNumber()
                +"   (" + phone.getType().name() +")");
            }
            if (peopleInfo.hasData() && peopleInfo.getData().is(Address.class)){
                Address address = peopleInfo.getData().unpack(Address.class);
                if (address.getHomeAddress()!=null){
                    System.out.println("家庭地址："+address.getHomeAddress());
                }
                if (address.getUnitAddress()!=null){
                    System.out.println("单位地址："+address.getUnitAddress());
                }
            }
//            if (peopleInfo.hasQq()){
//
//            }else if(peopleInfo.hasWechat()){
//
//            }
            switch (peopleInfo.getOtherContactCase()){
                case QQ:
                    System.out.println("QQ号："+peopleInfo.getQq());
                case WECHAT:
                    System.out.println("微信号："+peopleInfo.getWechat());
                case OTHERCONTACT_NOT_SET:
                    break;
            }

            for(Map.Entry<String, String> entry:peopleInfo.getRemarkMap().entrySet() ){
                System.out.println("  "+entry.getKey()+" : "+entry.getValue());
            }



        }
    }
}



















